<template>
  <div class="app-container">
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="教 练 名">
              <el-input style="width: 95%" v-model="searchObj.coachName" placeholder="教练名字"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()"
                     :disabled="$hasBP('bnt.coach.list') === false">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData"
                     :disabled="$hasBP('bnt.coach.list') === false">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 列表 -->
    <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;">

      <el-table-column label="序号" width="70" align="center" fixed="left">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="coachName" label="教练名" :show-overflow-tooltip="true" align="center"/>
      <el-table-column prop="phone" label="电话"  width="130" align="center"/>
      <el-table-column prop="storeName" label="门店" :show-overflow-tooltip="true" align="center"/>
      <el-table-column prop="sex" label="性别" align="center">
        <template slot-scope="scope">
          {{scope.row.sex === 0 ? '男' : '女'}}
        </template>
      </el-table-column>
      <el-table-column prop="skill" label="擅长" :show-overflow-tooltip="true" align="center"/>
      <el-table-column prop="price" label="费用" :show-overflow-tooltip="true" align="center"/>
      <el-table-column prop="count" label="上课数量" :show-overflow-tooltip="true" align="center"/>
      <el-table-column prop="selected" label="预约人数" align="center"/>
      <el-table-column prop="createTime" label="预约时间"  width="160" align="center"/>
      <el-table-column label="在职" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">在职</el-tag>
          <el-tag v-else type="danger">离职</el-tag>
        </template>
      </el-table-column>

      <!-- 点击详情 -->
      <el-dialog :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" label-width="170px" :inline="true">
          <template>
            <el-table :data="tableData" style="width: 100%">
              <el-table-column prop="name" label="姓名" align="center">
              </el-table-column>
              <el-table-column prop="phone" label="电话" align="center">
              </el-table-column>
              <el-table-column prop="sex" label="性别" align="center">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.sex === 0" type="success">女</el-tag>
                  <el-tag v-else-if="scope.row.sex === 1" type="success" >男</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="vip" label="截至时间" align="center" />
            </el-table>
          </template>
        </el-form>
      </el-dialog>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view"
                     @click="handleUpdate(scope.row)">查看预约</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination :current-page="page" :total="total" :page-size="limit"
                   style="padding: 30px 0; text-align: center;" layout="total, prev, pager, next, jumper"
                   @current-change="fetchData" />
  </div>
</template>

<script>
import api from '@/api/content/coach'
import apiAppointment from '@/api/content/appointment'

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      stores: [], // 门店列表
      tableData: [{
        name: null,
        vip: null,
        phone: null,
        sex: null
      }],

      createTimes: [],
      open: false
    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')
    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log('list mounted......')
  },

  methods: {
    // 加载banner列表数据
    fetchData(page = 1) {
      this.page = page
      apiAppointment.getPageCoachList(this.page, this.limit, this.searchObj).then(
        response => {
          this.list = response.data.records
          this.total = response.data.total
          // 数据加载并绑定成功
          this.listLoading = false
        }
      )
    },

    // 重置查询表单
    resetData() {
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    },
    handleUpdate(row){
      apiAppointment.getByCoachId(row.id).then(response =>{
        this.open = true
        this.tableData = response.data
        console.log(response.data)
      })
    }
  }
}
</script>
